//
//  LoginViewController.m
//  囧途
//
//  Created by fly on 16/3/3.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "LoginViewController.h"
#import <AVUser.h>
#import "ResignViewController.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameFiled;

@property (weak, nonatomic) IBOutlet UITextField *passWordFiled;

@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UIButton *resgiser;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title =@"登录";
}

- (IBAction)loginButtonPressed:(UIButton *)sender {
    [AVUser logInWithUsernameInBackground:_userNameFiled.text password:_passWordFiled.text block:^(AVUser *user, NSError *error) {
        
        if (user != nil ) {
            
            NSLog(@"user Is ======%@",user);
            
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        else{
            
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
        }];
            
            [alert addAction:alertAction];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    }];
}

- (IBAction)resgiserButtonPressed:(UIButton *)sender {
    
    ResignViewController * viewController = [[ResignViewController alloc]init];
    
    [self.navigationController pushViewController:viewController animated:YES];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
